#include "settings.h"
#include "ui_settings.h"

#include <QSettings>

#include "moveapplication.h"
#include <mlgesture.h>

using namespace mlgesture;

Settings::Settings(QWidget *parent) :
    QDialog(parent),
	ui_(new Ui::Settings)
{
	ui_->setupUi(this);
	loadSettings();
	connect(ui_->skinButton, SIGNAL(clicked()), this, SLOT(showSkinDialog()));
	skinDialog_ = NULL;
}

Settings::~Settings()
{
	delete ui_;
	if (skinDialog_) {
		delete skinDialog_;
	}
}

void Settings::accept() {
	if (saveSettings())
		if (skinDialog_) {
			delete skinDialog_;
			skinDialog_ = NULL;
		}
		QDialog::accept();
}

void Settings::reject() {
	if (skinDialog_) {
		delete skinDialog_;
		skinDialog_ = NULL;
	}
	QDialog::reject();
}

void Settings::loadSettings() {
	QSettings settings;
	settings.beginGroup(QString("camera"));
	ui_->camIdEdit->setText(settings.value("camId", "-1").toString());
	settings.endGroup();
	settings.beginGroup(QString("skinThreshold"));
	ui_->lowSkin->setText(settings.value("lowHue", MLGesture::getGlobalLowThreshold()*2).toString());
	ui_->upperSkin->setText(settings.value("upperHue", MLGesture::getGlobalUpperThreshold()*2).toString());
	ui_->lowValue->setText(settings.value("lowValue", MLGesture::getGlobalValueLowThreshold()).toString());
	ui_->upperValue->setText(settings.value("upperValue", MLGesture::getGlobalValueUpperThreshold()).toString());
	ui_->adaptBox->setChecked(settings.value("adaptThresholding", 1).toInt() ? true : false);
	settings.endGroup();
	settings.beginGroup(QString("tracking"));
	ui_->sensitySlider->setValue(settings.value("sensity", "0.9").toDouble() * 100);
	settings.endGroup();
}

bool Settings::saveSettings() {

	QString cam_id =  ui_->camIdEdit->text();
	bool ok;
	cam_id.toInt(&ok);
	if (!ok) {
		ui_->errorLabel->setText(tr("Identyfikator kamery musi być liczbą"));
		QPalette p;
		p.setColor(QPalette::Text, Qt::red);
		ui_->camIdEdit->setPalette(p);
		return false;
	}

	QSettings settings;
	settings.beginGroup(QString("camera"));
	settings.setValue(QString("camId"), cam_id);
	settings.endGroup();
	settings.beginGroup(QString("skinThreshold"));
	settings.setValue(QString("lowHue"), ui_->lowSkin->text().toInt());
	settings.setValue(QString("upperHue"), ui_->upperSkin->text().toInt());
	settings.setValue(QString("lowValue"), ui_->lowValue->text().toInt());
	settings.setValue(QString("upperValue"), ui_->upperValue->text().toInt());
	settings.setValue(QString("adaptThresholding"), ui_->adaptBox->isChecked() ? 1 : 0);
	settings.endGroup();
	settings.beginGroup(QString("tracking"));
	settings.setValue(QString("sensity"), ui_->sensitySpin->value() * 1. / 100);
	settings.endGroup();

	MoveApplication::instance()->loadSettings();
	return true;
}

void Settings::showSkinDialog() {
	if (!skinDialog_) {
		skinDialog_ = new SkinThresholdDialog(ui_->camIdEdit->text().toInt(), ui_->lowSkin->text().toInt(),
											  ui_->upperSkin->text().toInt(), ui_->lowValue->text().toInt(),
											  ui_->upperValue->text().toInt(), this);
		connect(skinDialog_, SIGNAL(accepted()), this, SLOT(saveSkin()));
	}
	skinDialog_->show();
}

void Settings::saveSkin() {
	if (skinDialog_) {
		ui_->lowSkin->setText(QString::number(skinDialog_->getLowThreshold()));
		ui_->upperSkin->setText(QString::number(skinDialog_->getUpperThreshold()));
		ui_->lowValue->setText(QString::number(skinDialog_->getValueLowThreshold()));
		ui_->upperValue->setText(QString::number(skinDialog_->getValueUpperThreshold()));
	}
}
